package com.mobi.spring1.controller;

import com.mobi.spring1.Sever.impl.AchievementSever;
import com.mobi.spring1.Sever.impl.UserSever;
import com.mobi.spring1.pojo.Achievement;
import com.mobi.spring1.pojo.User;
import io.swagger.annotations.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "成绩控制类")
@Controller
@RequestMapping("/achieve")
class AchievementController {

    @GetMapping("/hello")
    @ResponseBody
    public Achievement showHelloWorld(){
//        Map<String, Object> map = new HashMap<>();
//        map.put("msg", "HelloWorld");
//        map.put("a",new Achievement());
        return new Achievement();
    }


    @Autowired
    private AchievementSever as;


    //添加成绩
    @ApiOperation("为学生添加成绩")
    @PostMapping ("/add")
    @ResponseBody
    @ApiImplicitParam(name = "achievement",value = "学生成绩",dataType = "Achievement")
    public void add(Achievement achievement ,HttpServletResponse response){
/*        Map<String, String[]> map = request.getParameterMap();
        Achievement achievement = new Achievement();
        try {
            BeanUtils.populate(achievement,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }*/

        as.add(achievement);
        try {
            response.sendRedirect("/student_list.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("删除成绩")
    @GetMapping("/del")
    @ResponseBody
    public void del(@Param("id") Integer id, HttpServletResponse response){
        as.del(id);
        try {
            response.sendRedirect("/student_list.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("更新成绩")
    @GetMapping("/upd")
    @ResponseBody
    public void upd(@RequestBody Achievement achievement){
        as.add(achievement);
    }

    @ApiOperation("按照学生ID成绩")
    @GetMapping("/findById")
    @ResponseBody
    public Map<String, Object> findById(@RequestBody Integer id){
        Map<String, Object> map = new HashMap<>();
        Object byId = as.findById(id);
        map.put("findById",byId);
        return map;
    }

    //查询全部学生加成绩
    @ApiOperation("查询全部学生成绩")
    @GetMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(){
        Map<String, Object> map = new HashMap<>();
        //System.out.println(as);
        map.put("findByAll",as.findAll());
        return map;
    }
}
